/**
 * Created by GadflyBSD on 16/3/18.
 */
var meta = new Object();
var content = document.getElementsByTagName('meta');
var templatePath = '/Public/template/Business/';
for(var i = 0; i < content.length; i++){
	if(content[i].getAttribute('name') == 'Authcode')
		meta.appurl = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'appurl')
		meta.verifyImg = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'upload')
		meta.upload = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'angular')
		meta.angular = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'appId')
		meta.appId = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'timestamp')
		meta.timestamp = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'nonceStr')
		meta.nonceStr = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'signature')
		meta.signature = content[i].getAttribute('content');
}
angular.module('myApp', ['ionic', 'myApp.controllers', 'myApp.services', 'myApp.directive'])
.config(function($stateProvider, $urlRouterProvider){
	$stateProvider
		.state('shopCode', {
			url: '/shopCode',
			cache: false,
			templateUrl: '/Public/template/Business/shopCode.html',
			controller: 'shopCodeCtrl'
		})
		.state('createShop', {
			url: '/createShop',
			cache: false,
			templateUrl: '/Public/template/Business/createShop.html',
			controller: 'createShopCtrl'
		})
		.state('bindPerson', {
			url: '/bindPerson',
			cache: false,
			templateUrl: '/Public/template/Business/bindPerson.html',
			controller: 'bindPersonCtrl'
		})
		.state('orderList', {
			url: '/orderList',
			cache: false,
			templateUrl: '/Public/template/Business/orderList.html',
			controller: 'orderListCtrl'
		})
		.state('actionLog', {
			url: '/actionLog',
			cache: false,
			templateUrl: '/Public/template/Business/actionLog.html',
			controller: 'actionLogCtrl'
		})
		.state('personManage', {
			url: '/personManage',
			cache: false,
			templateUrl: '/Public/template/Business/personManage.html',
			controller: 'personManageCtrl'
		});
	$urlRouterProvider.otherwise('/shopCode');
})
.run(function($ionicPlatform){
	$ionicPlatform.ready(function(){
		if(window.cordova && window.cordova.plugins.Keyboard){
			cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
		}
		if(window.StatusBar){
			StatusBar.styleDefault();
		}
	});
});

angular.module('myApp.services')
.factory('myService', function($http){
	var param = {ac: 'angular'};
	return {
		category: function(){
			"use strict";
			return $http.post(meta.angular, {ac: 'angular', op: 'getCategory'});
		},
		district: function(){
			return $http.get('/Public/district.json');
		},
		getSession: function(path){
			"use strict";
			return $http.post(meta.angular, {ac: 'angular', op: 'getSession', path: path});
		},
		createShop: function(data){
			"use strict";
			return $http.post(meta.angular, _.extend({ac: 'angular', op: 'createShop'}, data));
		},
		qrcode2ShopInfo: function(qrcode){
			"通过二维码获得店铺信息";
			return $http.post(meta.angular, _.extend(param, {op: 'qrcode2ShopInfo', qrcode: qrcode}))
		},
		bindPerson: function(data){
			"收银员绑定商铺";
			return $http.post(meta.angular, _.extend(param, {op: 'bindPerson'}, data));
		}
	};
});

/*
angular.module('myApp.directive')
.directive('uploadWidget', function(){
	"use strict";
	return {
		restrict: 'EA',
		template: function(elem, attr){
			switch(attr.myWidget){
				case 'Logo':
					return '<img src="" ng-mouseover="uploadShow()" ng-click="uploadButton()">';
				case 'Banner':
					return '<img src="" ng-mouseover="uploadShow()" ng-click="uploadButton()">';
			}
		},
		scope: {src: '@forSrc'},
		transclude: true,
		controller: function($scope){
			$scope.uploadShow = function(){}
			$scope.uploadButton = function(){}
		},
		link: function(scope, element, attr){
			angular.element(element).attr('src', scope.src);
		}
	}
});*/

angular.module('myApp.controllers')
	.controller('shopCodeCtrl', function($scope, myService, $ionicPopup, $state){
		"店铺二维码页面控制器";
		myService.getSession().success(function(resp){
			if(resp.isbind == 0){
				var alertPopup = $ionicPopup.alert({
					title: '出错啦!',
					template: '您还没有创建商铺或者没有绑定商铺!'
				});
				alertPopup.then(function(res){
					$state.go('createShop');
				});
			}else{
				$scope.shopTitle = resp.title;
				$scope.qrcodeUrl = resp.qrcodeUrl;
			}
		});
	})
	.controller('createShopCtrl', function($scope, myService, $ionicPopup, $state){
		"创建店铺页面控制器";
		myService.getSession().success(function(resp){
			if(resp.isbind != 0){
				var alertPopup = $ionicPopup.alert({
					title: '出错啦!',
					template: '您已经创建过商铺或者绑定过商铺!请不要重复操作!'
				});
				alertPopup.then(function(res){
					$state.go('shopCode');
				});
			}else{
				myService.category().success(function(cate){
					"获得店铺分类数据";
					$scope.category = cate;
				});
				$scope.submit = function(){
					myService.createShop($scope.formData).success(function(resp){
						"创建店铺表单提交";
						var alertPopup = $ionicPopup.alert({
							title: resp.title,
							template: resp.content
						});
						alertPopup.then(function(res){
							$state.go('shopCode');
						});
					})
				};
			}
		});
	})
	.controller('bindPersonCtrl', function($scope, myService, $ionicPopup, $state){
		"收银员绑定店铺控制器";
		myService.getSession().success(function(resp){
			if(resp.isbind != 0){
				var alertPopup = $ionicPopup.alert({
					title: '出错啦!',
					template: '您已经创建过商铺或者绑定过商铺!请不要重复操作!'
				});
				alertPopup.then(function(res){
					$state.go('shopCode');
				});
			}else{
				wx.config({
					debug: false,
					appId: meta.appId,
					timestamp: meta.timestamp,
					nonceStr: meta.nonceStr,
					signature: meta.signature,
					jsApiList: ['scanQRCode']
				});
				wx.ready(function(){
					$scope.scanQrCode = function(){
						wx.scanQRCode({
							needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
							scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
							success: function(res){
								myService.qrcode2ShopInfo(res.resultStr).success(function(resp){
									$scope.shopInfo = resp;
								});
							}
						});
					}
					$scope.submit = function(){
						var data = _.extend({sid: $scope.shopInfo.sid}, $scope.formData);
						myService.bindPerson(data).success(function(resp){
							"收银员绑定店铺表单提交";
							var alertPopup = $ionicPopup.alert({
								title: resp.title,
								template: resp.content
							});
							alertPopup.then(function(res){
								$state.go('shopCode');
							});
						});
					}
				});
			}
		});
	})
	.controller('orderListCtrl', function($scope){
	})
	.controller('actionLogCtrl', function($scope){
	})
	.controller('personManageCtrl', function($scope){
	})
	.controller('shopInfoCtrl', function($scope){
		"use strict";
		$scope.uploadBanner = function(){
			alert(1);
		}
	})
	.controller('NavCtrl', function($scope, $ionicActionSheet, $state){
		$scope.goShopCode = function(){
			$state.go('shopCode');
		}
		$scope.openMenu1 = function(){
			$ionicActionSheet.show({
				buttons: [
					{text: '创建店铺'},
					{text: '员工绑定'},
					{text: '订单列表'},
					{text: '操作日志'}
				],
				titleText: '商户功能子菜单',
				buttonClicked: function(index){
					switch(index){
						case 0:
							$state.go('createShop');
							break;
						case 1:
							$state.go('bindPerson');
							break;
						case 2:
							$state.go('orderList');
							break;
						case 3:
							$state.go('actionLog');
					}
					return true;
				}
			});
		};
		$scope.openMenu2 = function(){
			$ionicActionSheet.show({
				buttons: [
					{text: '员工管理'},
					{text: '店铺详情管理'},
					{text: '统计报表'},
					{text: '绑定银行卡'},
					{text: '修改老板密码'},
				],
				titleText: '老板后台子菜单',
				buttonClicked: function(index){
					switch(index){
						case 0:
							$state.go('personManage');
							break;
						case 1:
							$state.go('shopInfo');
							break;
						case 2:
							$state.go('statiscalForm');
							break;
						case 3:
							$state.go('bindBank');
							break;
						case 4:
							$state.go('bossPassword');
					}
					return true;
				}
			});
		};
	});